package com.bl.weilan.manager.controller.file;

import com.bl.weilan.common.utils.JSONUtil;
import com.bl.weilan.manager.controller.BaseController;
import com.bl.weilan.common.ApiResponseBody;
import com.bl.weilan.service.biz.file.FileService;
import com.bl.weilan.service.vo.file.FileInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by liangweikang on 2017/5/15.
 */
@RequestMapping(value = {"/weilan/manager/file"})
@Api(value = "文件", description = "文件相关api")
@Controller
@Log4j2
public class FileController extends BaseController {

    @Autowired
    FileService fileService;

    @ApiOperation(value = "上传文件api", notes = "通过multipart/form-data的方式上传，返回文件")
    @RequestMapping(value = {"/upload"}, method = {POST})
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ApiResponseBody upload(@ApiParam(required = true, value = "文件")
                                  @RequestParam(value = "file", required = true) MultipartFile file,
                                  HttpServletRequest request) {


        String fileName = file.getOriginalFilename();
        log.info("adminInfo: " + JSONUtil.jsonEncode(getUserInfo(request)));

        FileInfoVo imgInfoVo = fileService.upload(file, getUserInfo(request).getAccount());

        return ApiResponseBody.createSuccessBody(imgInfoVo);
    }


}
